#include<iostream>

using namespace std;


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    void Inorder(TreeNode* root, int& count, int& ret)
    {
        if (root == nullptr || count <= 0)return;
        Inorder(root->left, count, ret);
        count--;
        if (count == 0)
        {
            ret = root->val;
        }
        Inorder(root->right, count, ret);

    }
    int kthSmallest(TreeNode* root, int k) {
        int count = k;
        int ret = 0;
        Inorder(root, count, ret);
        return ret;
    }
};